package rottenbren.dev.android.diary.activity;

import java.util.ArrayList;
import java.util.Calendar;

import rottenbren.dev.android.diary.adapter.CalendarAdapter;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

public class CalendarActivity extends BaseActivity {

	private TextView monthTitle;
	private TextView dayText;
	private Button preMonthBtn;
	private Button nextMonthBtn;
	private GridView calendarGrid;
	
	private ArrayList<String> dayList;
	private CalendarAdapter dayAdapter;
	
	private Calendar cal = Calendar.getInstance();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.calendar);
		
		monthTitle = (TextView) findViewById(R.id.monthTitle);
		preMonthBtn = (Button) findViewById(R.id.preMonthBtn);
		nextMonthBtn = (Button) findViewById(R.id.nextMonthBtn);
		calendarGrid = (GridView) findViewById(R.id.calendarGrid);

		dayList = new ArrayList<String>();
		cal.set(Calendar.DATE, 1);
		
		setCalendar();
		
		preMonthBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				cal.add(Calendar.MONTH, -1);
				setCalendar();
			}
		});
		
		nextMonthBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				cal.add(Calendar.MONTH, 1);
				setCalendar();
			}
		});

	}
	
	private void setCalendar(){
		monthTitle.setText(cal.get(Calendar.YEAR) + " 년 " + (cal.get(Calendar.MONTH) + 1) + " 월");
		
		dayList.clear();
		
		int firstDay = cal.get(Calendar.DAY_OF_WEEK)-1;
		int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		dayList.add("일");
		dayList.add("월");
		dayList.add("화");
		dayList.add("수");
		dayList.add("목");
		dayList.add("금");
		dayList.add("토");
		
		
		for(int i=0; i < firstDay; i++){
			dayList.add("");
		}
		
		for(int i=0; i < lastDay; i++){
			dayList.add((i+1)+"");
		}
		

		dayAdapter = new CalendarAdapter(CalendarActivity.this, R.layout.day_gridtext, dayList);
			gridCalendar.setAdapter(dayAdapter);

	}
	
}
